package zzs.generalframework.coder.common.model;

import java.util.List;

import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-coder
 * @package zzs.generalframework.coder.common.model
 * @author zzs
 * @date 2018年1月31日
 * @version 1.0
 */
public class MethodInfo
{
	private boolean isInterface;
	private String note;
	private AnnotationInfo annotationInfo;
	private String accessModifier;
	private String returnType;
	private String methodName;
	private List<Class> parameterList;
	private List<Class> exceptionList;
	private String methodBody;

	public void setInterface(boolean isInterface)
	{
		this.isInterface = isInterface;
	}

	public boolean isInterface()
	{
		return isInterface;
	}

	public String getNote()
	{
		return note;
	}

	public void setNote(String note)
	{
		this.note = note;
	}

	public AnnotationInfo getAnnotationInfo()
	{
		return annotationInfo;
	}

	public void setAnnotationInfo(AnnotationInfo annotationInfo)
	{
		this.annotationInfo = annotationInfo;
	}

	public String getAccessModifier()
	{
		return accessModifier;
	}

	public void setAccessModifier(String accessModifier)
	{
		this.accessModifier = accessModifier;
	}

	public String getReturnType()
	{
		return returnType;
	}

	public void setReturnType(String returnType)
	{
		this.returnType = returnType;
	}

	public String getMethodName()
	{
		return methodName;
	}

	public void setMethodName(String methodName)
	{
		this.methodName = methodName;
	}

	public List<Class> getParameterList()
	{
		return parameterList;
	}

	public void setParameterList(List<Class> parameterList)
	{
		this.parameterList = parameterList;
	}

	public List<Class> getExceptionList()
	{
		return exceptionList;
	}

	public void setExceptionList(List<Class> exceptionList)
	{
		this.exceptionList = exceptionList;
	}

	public String getMethodBody()
	{
		return methodBody;
	}

	public void setMethodBody(String methodBody)
	{
		this.methodBody = methodBody;
	}

	@Override
	public String toString()
	{
		initNote();
		StringBuffer methodBuffer = new StringBuffer();
		StringBuffer noteBuffer = new StringBuffer();
		if (StringUtils.isNotNullAndBlank(note))
		{
			noteBuffer.append(note).append("\n");
		}
		methodBuffer.append(noteBuffer);
		StringBuffer annotationBuffer = new StringBuffer();
		if (annotationInfo != null)
		{
			annotationBuffer.append(annotationInfo.toString()).append("\n");
		}
		methodBuffer.append(annotationBuffer);

		StringBuffer methodTitleBuffer = new StringBuffer();
		if (StringUtils.isNotNullAndBlank(accessModifier))
		{
			methodTitleBuffer.append(accessModifier);
		}
		else
		{
			methodTitleBuffer.append(" ").append("public");
		}
		if (StringUtils.isNotNullAndBlank(returnType))
		{
			methodTitleBuffer.append(returnType);
		}
		else
		{
			methodTitleBuffer.append(" ").append("void");
		}
		methodTitleBuffer.append(" ").append(methodName);

		methodTitleBuffer.append("(");
		if (parameterList != null && parameterList.size() > 0)
		{
			StringBuffer methodParameterBuffer = new StringBuffer();
			for (Class clazz : parameterList)
			{
				String clazzSimpleName = clazz.getSimpleName();
				methodParameterBuffer.append(clazzSimpleName).append(" ").append(clazzSimpleName.substring(0, 1).toLowerCase()).append(clazzSimpleName.substring(1));
			}
			methodTitleBuffer.append(methodParameterBuffer);
		}
		methodTitleBuffer.append(")");

		if (exceptionList != null && exceptionList.size() > 0)
		{
			StringBuffer exceptionBuffer = new StringBuffer();
			for (int i = 0; i < exceptionList.size(); i++)
			{
				if (i < (exceptionList.size() - 1))
				{
					exceptionBuffer.append(exceptionList.get(i).getSimpleName()).append(",");
				}
				else
				{
					exceptionBuffer.append(exceptionList.get(i).getSimpleName());
				}
			}
			if (!StringUtils.isNotNullAndBlank(exceptionBuffer.toString()))
			{
				methodTitleBuffer.append("throws").append(" ").append(exceptionBuffer);
			}
		}

		methodBuffer.append(methodTitleBuffer);

		StringBuffer methodBodyBuffer = new StringBuffer();
		if (isInterface)
		{
			methodBodyBuffer.append(";").append("\n");
		}
		else
		{
			methodBodyBuffer.append("\n");
			methodBodyBuffer.append("{").append(methodBody).append("}");
		}
		methodBuffer.append(methodBodyBuffer);
		return methodBuffer.toString();
	}

	private void initNote()
	{
		StringBuffer noteBuffer = new StringBuffer();
		noteBuffer.append("/**").append("\n");
		noteBuffer.append(" *").append("\n");
		if (parameterList != null && parameterList.size() > 0)
		{
			for (Class clazz : parameterList)
			{
				noteBuffer.append(" * @param ").append(clazz.getSimpleName().substring(0, 1).toLowerCase()).append(clazz.getSimpleName().substring(1)).append("\n");
			}
		}
		if (StringUtils.isNotNullAndBlank(returnType))
		{
			if (!"void".equals(returnType))
			{
				noteBuffer.append(" * @return").append("\n");
			}
		}
		if (exceptionList != null && exceptionList.size() > 0)
		{
			for (Class clazz : parameterList)
			{
				noteBuffer.append(" * @throws ").append(clazz.getSimpleName()).append("\n");
			}
		}

		noteBuffer.append(" */");
		this.note = noteBuffer.toString();

	}

}
